using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using WindowsGame1.gamePlugins.renderer;
using WindowsGame1;
using Cookie_Hero.physics;
using Cookie_Hero.graphics;
using Cookie_Hero.events;
using Cookie_Hero.communications;


namespace Cookie_Hero.testing
{
    public enum Level
    {
        MENU = 0,
        HOMESCREEN = 1,
        PLATFORM = 2
    }

    public class Testing : Microsoft.Xna.Framework.GameComponent
    {
        GameContainer container;
        GameDisplay gameDisplay;
        GraphicsDeviceManager graphics;
        Physics physics;
        FPS fps;


        public Testing(Game game, ContentManager Content, GraphicsDeviceManager graphics) : base(game)
        {
            gameDisplay = new GameDisplay();
            container = new GameContainer(Content);
            physics = new Physics(Content);
            fps = new FPS(Content);
            this.graphics = graphics;
        }

        
        public override void Initialize()
        {
            gameDisplay.setWindow(graphics, GameDisplay.Display.FULLSCREEN_MODE_1366);
            //animations = db.GetAllAnimations(animations);
            //objects = db.GetAllObjectsFromLevel((int) Level.HOMESCREEN);
            //stageEvents = db.GetAllEventsFromLevel((int) Level.HOMESCREEN);
            container.initLevel((int) Level.HOMESCREEN);
            Log.V(container.objects.Count + " objects loaded, " + container.animations.Lenght + " animations loaded, 0 events loaded");
            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            //lock (container)
            //{
                // TODO create method for events
                //container = mouseEvents.update(container);
                //container = keyboardEvents.update(container);
                
                container.update(gameTime, physics, gameDisplay);
            //}
                
            base.Update(gameTime);
        }


        public void Draw (SpriteBatch spriteBatch)
        {
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            for (int i = container.objects.Count - 1; i >= 0; i--)
            {
                PhysicsObject obj = container.objects[i];
                if (PhysicsOperation.isVisual(obj))
                {
                    Texture2D texture = container.animations.getSequenceImage(obj.aniTexture);
                    if (texture != null)
                    {
                        spriteBatch.Draw(texture, obj.position, null, Color.White, obj.rotation, obj.origin, obj.scale, obj.effects, (0.1F * (float)obj.layerDepth));
                    }
                }
            }
            spriteBatch.End();
            fps.draw(spriteBatch);
        }
    }
}
